package com.hdoit.ares.web.cust.wechat;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.cust.model.CustFootprint;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustFootprintService;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.wechat.BaseWechat;

@Controller
@RequestMapping(value="/wechat/cust/colAndFoot")
public class CustFootprintWechat extends BaseWechat{

	@Override
	public String getRoutePath() {
		
		return "areas/wechat/cust/colAndFoot/";
	}
	
	private ICustFootprintService custFootprintService;
	
	public ICustFootprintService getCustFootprintService() {
		return custFootprintService;
	}
	
	@Resource
	public void setCustFootprintService(ICustFootprintService custFootprintService) {
		this.custFootprintService = custFootprintService;
	}
	
	private IWithdrawParamService withdrawParamService;

	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}

	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	private final String addFootprintViewName = "addFootprint";
	private final String deleteFootprintViewName = "deleteFootprint";
	private final String footprintListViewName = "footprintList";
	
	@RequestMapping(value=footprintListViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String footprintList(Model model,String type, Integer pageNum){
		System.err.println("------------------------"+type);
		System.err.println("传过来的页面++++++++++++++"+pageNum);
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel); 
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		System.err.println("ididididiid"+customer.getId());
		WithdrawParam wp = withdrawParamService.queryModelByHelpCode("pageSize");
		Integer length = wp.getParam();
		System.err.println("长度++++++++++++++++"+length);
		
		List<Artical> articals=custFootprintService.selectArticalByType(type, customer.getId(),pageNum,length);
		
		List<Artical> allArticals=custFootprintService.selectArticalByType(type, customer.getId(),null,null);
		int allPage = (int)Math.ceil((double)allArticals.size()/(double)length);
		System.err.println("---------------------------"+allPage);
		
		model.addAttribute("allPage",allPage);
		model.addAttribute("articals",articals);
		return view(footprintListViewName);
	}
	
	@RequestMapping(value=addFootprintViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String addCollect(Model model,String type,String thingId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel); 
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		
		CustFootprint  custFootprint = new CustFootprint();
		custFootprint.setId(CommonUtils.getKeyId());
		custFootprint.setType(type);
		custFootprint.setThingId(thingId);
		custFootprint.setCustId(customer.getId());
		custFootprint.setBrowseDate(DateUtils.getCurrent());
		
		custFootprint.setModelStatus(ModelStatus.add);
		custFootprintService.applyChange(custFootprint);
		return "true";
	}
	
	@ResponseBody
	@RequestMapping(value=deleteFootprintViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String deleteFootprintByType(Model model,String type){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel); 
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		
		int result = custFootprintService.deleteFootprint(type, customer.getId());
		
		if(result>0){
			return "true";
		}else{
			return "false";
		}
	}
	
	
}
